// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.ui.dialogs;

import org.dengues.reports.DenguesReportsPlugin;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-16 qiang.zhang $
 * 
 */
public class TableOptionDialog extends TrayDialog {

    private static final String MSG_DATA_SET = "Data Set:";

    private static final String MSG_REMEMBER_DIMENSIONS_FOR_NEW_GRIDS = "&Use these dimensions for new grids.";

    private static final String MSG_REMEMBER_DIMENSIONS_FOR_NEW_TABLES = "&Use these dimensions for new tables.";

    private static final String MSG_NUMBER_OF_GRID_ROWS = "Number of &rows:";

    private static final String MSG_NUMBER_OF_TABLE_ROWS = "Number of &rows:";

    private static final String MSG_NUMBER_OF_COLUMNS = "Number of &columns:";

    private static final String MSG_GRID_SIZE = "Grid size";

    private static final String MSG_TABLE_SIZE = "Table size";

    private static final String MSG_INSERT_GRID = "Insert Grid";

    private static final String MSG_INSERT_TABLE = "Insert Table";

    private static final String NONE = "None";

    private static final int DEFAULT_TABLE_ROW_COUNT = 3;

    private static final int DEFAULT_ROW_COUNT = 3;

    private static final int DEFAULT_COLUMN_COUNT = 3;

    /**
     * Comment for <code>DEFAULT_TABLE_ROW_COUNT_KEY</code>
     */
    public static final String DEFAULT_TABLE_ROW_COUNT_KEY = "Default table row count"; //$NON-NLS-1$

    /**
     * Comment for <code>DEFAULT_TABLE_COLUMN_COUNT_KEY</code>
     */
    public static final String DEFAULT_TABLE_COLUMN_COUNT_KEY = "Default table column count"; //$NON-NLS-1$

    /**
     * Comment for <code>DEFAULT_GRID_ROW_COUNT_KEY</code>
     */
    public static final String DEFAULT_GRID_ROW_COUNT_KEY = "Default grid row count"; //$NON-NLS-1$

    /**
     * Comment for <code>DEFAULT_GRID_COLUMN_COUNT_KEY</code>
     */
    public static final String DEFAULT_GRID_COLUMN_COUNT_KEY = "Default grid column count"; //$NON-NLS-1$

    private Spinner rowEditor;

    private Spinner columnEditor;

    private Button chkbox;

    private int rowCount, columnCount;

    private boolean insertTable = true;

    private Combo dataSetCombo;

    protected Object result;

    /**
     * Qiang.Zhang.Adolf@gmail.com TableOptionDialog constructor comment.
     * 
     * @param shell
     */
    public TableOptionDialog(Shell shell, boolean insertTable) {
        super(shell);
        this.insertTable = insertTable;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
     */
    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText(MSG_INSERT_TABLE);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com TableOptionDialog constructor comment.
     * 
     * @param parentShell
     */
    public TableOptionDialog(IShellProvider parentShell) {
        super(parentShell);
    }

    private void loadPreference() {
        if (insertTable) {
            columnCount = DenguesReportsPlugin.getDefault().getPreferenceStore().getInt(DEFAULT_TABLE_COLUMN_COUNT_KEY);
            rowCount = DenguesReportsPlugin.getDefault().getPreferenceStore().getInt(DEFAULT_TABLE_ROW_COUNT_KEY);
        } else {
            columnCount = DenguesReportsPlugin.getDefault().getPreferenceStore().getInt(DEFAULT_GRID_COLUMN_COUNT_KEY);
            rowCount = DenguesReportsPlugin.getDefault().getPreferenceStore().getInt(DEFAULT_GRID_ROW_COUNT_KEY);
        }

        if (columnCount <= 0) {
            columnCount = DEFAULT_COLUMN_COUNT;
        }
        if (rowCount <= 0) {
            rowCount = insertTable ? DEFAULT_TABLE_ROW_COUNT : DEFAULT_ROW_COUNT;
        }

    }

    private void savePreference() {
        if (insertTable) {
            DenguesReportsPlugin.getDefault().getPreferenceStore().setValue(DEFAULT_TABLE_COLUMN_COUNT_KEY, columnCount);
            DenguesReportsPlugin.getDefault().getPreferenceStore().setValue(DEFAULT_TABLE_ROW_COUNT_KEY, rowCount);
        } else {
            DenguesReportsPlugin.getDefault().getPreferenceStore().setValue(DEFAULT_GRID_COLUMN_COUNT_KEY, columnCount);
            DenguesReportsPlugin.getDefault().getPreferenceStore().setValue(DEFAULT_GRID_ROW_COUNT_KEY, rowCount);
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        loadPreference();

        Composite composite = (Composite) super.createDialogArea(parent);
        ((GridLayout) composite.getLayout()).numColumns = 2;

        new Label(composite, SWT.NONE).setText(insertTable ? MSG_TABLE_SIZE : MSG_GRID_SIZE);
        Label sp = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
        sp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        Composite innerPane = new Composite(composite, SWT.NONE);
        GridData gdata = new GridData(GridData.FILL_BOTH);
        gdata.horizontalSpan = 2;
        innerPane.setLayoutData(gdata);
        GridLayout glayout = new GridLayout(2, false);
        glayout.marginWidth = 10;
        innerPane.setLayout(glayout);

        new Label(innerPane, SWT.NONE).setText(MSG_NUMBER_OF_COLUMNS);
        columnEditor = new Spinner(innerPane, SWT.BORDER);
        columnEditor.setMinimum(1);
        columnEditor.setMaximum(Integer.MAX_VALUE);
        columnEditor.setIncrement(1);
        columnEditor.setSelection(columnCount);
        columnEditor.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        new Label(innerPane, SWT.NONE).setText(insertTable ? MSG_NUMBER_OF_TABLE_ROWS : MSG_NUMBER_OF_GRID_ROWS);
        rowEditor = new Spinner(innerPane, SWT.BORDER);
        rowEditor.setMinimum(1);
        rowEditor.setMaximum(Integer.MAX_VALUE);
        rowEditor.setIncrement(1);
        rowEditor.setSelection(rowCount);
        rowEditor.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        if (insertTable) {
            new Label(innerPane, SWT.NONE).setText(MSG_DATA_SET);
            dataSetCombo = new Combo(innerPane, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
            dataSetCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
            String[] dataSets = new String[0];
            String[] newList = new String[dataSets.length + 1];
            System.arraycopy(dataSets, 0, newList, 1, dataSets.length);
            newList[0] = NONE;
            dataSetCombo.setItems(newList);
            dataSetCombo.select(0);
        }

        else {
            Label lb = new Label(composite, SWT.NONE);
            gdata = new GridData(GridData.FILL_HORIZONTAL);
            gdata.horizontalSpan = 2;
            lb.setLayoutData(gdata);
        }
        chkbox = new Button(composite, SWT.CHECK);
        chkbox.setText(insertTable ? MSG_REMEMBER_DIMENSIONS_FOR_NEW_TABLES : MSG_REMEMBER_DIMENSIONS_FOR_NEW_GRIDS);
        gdata = new GridData(GridData.FILL_HORIZONTAL);
        gdata.horizontalSpan = 2;
        chkbox.setLayoutData(gdata);

        return composite;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#okPressed()
     */
    @Override
    protected void okPressed() {
        rowCount = rowEditor.getSelection();
        columnCount = columnEditor.getSelection();

        if (columnCount <= 0) {
            columnCount = DEFAULT_COLUMN_COUNT;
        }
        if (rowCount <= 0) {
            rowCount = insertTable ? DEFAULT_TABLE_ROW_COUNT : DEFAULT_ROW_COUNT;
        }

        if (insertTable) {
            setResult(new Object[] { new Integer(columnCount), new Integer(rowCount),
                    dataSetCombo.getItem(dataSetCombo.getSelectionIndex()).toString() });
        } else
            setResult(new Object[] { new Integer(columnCount), new Integer(rowCount) });

        if (chkbox.getSelection()) {
            savePreference();
        }
        super.okPressed();
    }

    /**
     * Getter for result.
     * 
     * @return the result
     */
    public Object getResult() {
        return this.result;
    }

    /**
     * Sets the result.
     * 
     * @param result the result to set
     */
    public void setResult(Object result) {
        this.result = result;
    }

}
